package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SchoolClasses;
import com.ruoyi.system.api.domain.SchoolStudent;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SchoolStudentTj;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.ISchoolStudentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生所属班级Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-19
 */
@Service
@Transactional
public class SchoolStudentServiceImpl implements ISchoolStudentService 
{
    @Autowired
    private SchoolStudentMapper schoolStudentMapper;

    @Autowired
    private SchoolClassesMapper schoolClassesMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询学生所属班级
     * 
     * @param id 学生所属班级ID
     * @return 学生所属班级
     */
    @Override
    public SchoolStudent selectSchoolStudentById(Long id)
    {
        return schoolStudentMapper.selectSchoolStudentById(id);
    }

    /**
     * 查询学生所属班级列表
     * 
     * @param schoolStudent 学生所属班级
     * @return 学生所属班级
     */
    @Override
    public List<SchoolStudent> selectSchoolStudentList(SchoolStudent schoolStudent)
    {
        return schoolStudentMapper.selectSchoolStudentList(schoolStudent);
    }

    @Override
    public List<SchoolStudentTj> selectSchoolStudentListTj(SchoolStudent schoolStudent)
    {
        return schoolStudentMapper.selectSchoolStudentListTj(schoolStudent);
    }

    @Override
    public List<SchoolStudentTj> selectSchoolStudentListPmTj(SchoolStudent schoolStudent)
    {
        List<SchoolStudentTj> list = schoolStudentMapper.selectSchoolStudentListPmTj(schoolStudent);
        for (SchoolStudentTj item:list) {
            SchoolClasses schoolClasses = schoolClassesMapper.selectSchoolClassesById(Long.valueOf(item.getClassesId()));
            item.setClassesName(schoolClasses.getClassesName());
        }
        return list;
    }

    /**
     * 新增学生所属班级
     * 
     * @param schoolStudent 学生所属班级
     * @return 结果
     */
    @Override
    public int insertSchoolStudent(SchoolStudent schoolStudent)
    {
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        schoolStudent.setTeacherId(userId);
        schoolStudent.setCreateTime(DateUtils.getNowDate());
        return schoolStudentMapper.insertSchoolStudent(schoolStudent);
    }

    /**
     * 修改学生所属班级
     * 
     * @param schoolStudent 学生所属班级
     * @return 结果
     */
    @Override
    public int updateSchoolStudent(SchoolStudent schoolStudent)
    {
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        schoolStudent.setTeacherId(userId);
        return schoolStudentMapper.updateSchoolStudent(schoolStudent);
    }

    @Override
    @Transactional
    public int updateSchoolStudentAll(List<SchoolStudent> list)
    {
        SchoolStudent item = list.get(0);
        schoolStudentMapper.deleteSchoolStudentByClassesId(item.getClassesId());
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        SysPost info = postMapper.getPostCodeUnique("xuesheng");
        for (SchoolStudent obj:list) {
            obj.setTeacherId(userId);
            obj.setCreateTime(DateUtils.getNowDate());
            schoolStudentMapper.insertSchoolStudent(obj);
            userPostMapper.deleteUserPostByUserId(obj.getStudentId());
            List<SysUserPost> postList = new ArrayList<SysUserPost>();
            SysUserPost up = new SysUserPost();
            up.setUserId(obj.getStudentId());
            up.setPostId(info.getPostId());
            postList.add(up);
            userPostMapper.batchUserPost(postList);
            SysUser user = new SysUser();
            user.setUserId(obj.getStudentId());
            user.setDeptId(obj.getSchoolId());
            userMapper.updateUser(user);
        }
        return list.size();
    }

    /**
     * 批量删除学生所属班级
     * 
     * @param ids 需要删除的学生所属班级ID
     * @return 结果
     */
    @Override
    public int deleteSchoolStudentByIds(Long[] ids)
    {
        return schoolStudentMapper.deleteSchoolStudentByIds(ids);
    }

    /**
     * 删除学生所属班级信息
     * 
     * @param id 学生所属班级ID
     * @return 结果
     */
    @Override
    public int deleteSchoolStudentById(Long id)
    {
        return schoolStudentMapper.deleteSchoolStudentById(id);
    }
}
